package cate.game.role.fight;

import cate.common.game.GameCollection;
import cate.common.table.d.GDPosition;
import cate.common.table.item.hero.row.HeroBaseRow;
import cate.game.fight.msg.FuncPositionUpdateResp;
import cate.game.role.Role;
import cate.game.role.RolePart;
import cate.game.role.bag.hero.Hero;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Document(collection = GameCollection.NAME_ROLE_FIGHT)
public class RoleFight extends RolePart {

    public RoleFight() {}

    public RoleFight(Role role) {
        super(role);
    }

    @JsonIgnore
    @Transient
    public FightAction action = new FightAction();

    @NoteField(value = "阵法配置",detail = "Map<功能id，具体的阵法配置>")
    public Map<Integer,FuncPosition> funcPositions;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (funcPositions == null) {
            funcPositions = new HashMap<>();
        }
        removeInvalidPosition();
    }

    private void removeInvalidPosition() {
        for (FuncPosition value : funcPositions.values()) {
            value.positions.removeIf(a -> getRole().getGame().table.position.funcPosition.findRow(value.funcId, a.posNum) == null);
        }
    }

    public FuncPosition clearPosition(int funcId){
        FuncPosition funcPosition = funcPositions.get(funcId);
        if(funcPosition != null){
            funcPosition.positions.clear();
        }
        return funcPosition;
    }

    /**
     * 获取一个功能中的一个编队的布阵
     * @param funcId
     * @param num
     * @return
     */
    @JsonIgnore
    public Position getPosition(int funcId, int num) {
        FuncPosition funcPosition = funcPositions.get(funcId);
        if (funcPosition == null) {
            return null;
        }
        return funcPosition.getPosition(num);
    }


    /**
     * 获取一个功能的完整布阵信息
     * @param funcId
     * @return
     */
    @JsonIgnore
    public FuncPosition getFuncPositions(int funcId){
        return funcPositions.get(funcId);
    }

    @JsonIgnore
    public Position getPosition(int funcId) {
        return getPosition(funcId, GDPosition.DEFAULT_POS_NUM);
    }

    public void addPosition(FuncPosition funcPosition) {
        funcPositions.put(funcPosition.funcId, funcPosition);
    }

    public void actionForFuncPosition(Consumer<Hero> consumer , int funcId){
        FuncPosition funcPosition = funcPositions.get(funcId);
        if(funcPosition != null){
            for(Position position : funcPosition.positions){
                for(HeroGrid grid : position.grids){
                    Hero hero = role.getBag().hero.getItem(grid.uid);
                    if(hero != null ){
                        consumer.accept(hero);
                    }
                }
            }
        }
    }

    /**
     * 删除特定功能的布阵
     * @param funcId
     */
    public void removeFuncPosition(int funcId) {
        FuncPosition funcPosition = funcPositions.get(funcId);
        if (funcPosition == null) {
            return;
        }
        funcPosition.clear();
        role.sendNow(new FuncPositionUpdateResp(funcPosition));
    }

    /**
     * 获取最大阵容设置数量
     */
    @JsonIgnore
    public int maxFuncPositionNum() {
        int maxNum = 0;
        for (FuncPosition funcPosition : funcPositions.values()) {
            maxNum = Math.max(maxNum, funcPosition.positions.size());
        }
        return maxNum;
    }

    /**
     * 布阵中是否存在对应的原型ID
     *
     * @param funcPosition 功能布阵
     * @param protoIds 原型ID列表
     */
    public boolean funcPositionContainsHeroProtoId(FuncPosition funcPosition, List<Integer> protoIds) {
        for (Position position : funcPosition.positions) {
            for (HeroGrid grid : position.grids) {
                Hero hero = role.getBag().hero.getItem(grid.uid);
                if (hero != null) {
                    HeroBaseRow config = hero.getTpl(role.toPlayBuildContext());
                    if (protoIds.contains(config.protoId)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 布阵中是否存在对应的圣器ID
     *
     * @param funcPosition 功能布阵
     * @param hallows 圣器ID列表
     */
    public boolean funcPositionContainsHallow(FuncPosition funcPosition, List<Integer> hallows) {
        for (Position position : funcPosition.positions) {
            if (position.hallowType != 0 && hallows.contains(position.hallowType)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void offline(){
        super.offline();
        action.offline();
    }
}
